package com.eascs.p2p.admin.manager.impl;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.p2p.admin.manager.IAdminUserManager;
import com.eascs.p2p.common.dao.BaseDao;
import com.eascs.p2p.common.entity.AdminRoleEntity;
import com.eascs.p2p.common.entity.AdminUserEntity;
import com.eascs.p2p.common.entity.AdminUserRoleEntity;
import com.eascs.p2p.common.vo.AdminUserVO;

import jodd.util.StringUtil;

@Repository
public class AdminUserManagerImpl extends BaseDao implements IAdminUserManager {

	
	
	/*@Override
	public <T extends User> T getUserByAccount(String loginName, Class<T> type) {
		String sql = "select   p.id as no ,p.name as cname ,p.mobile as mobile,p.account as account  from p2p_admin_user p where p.account = ? ";
		return this.findEntity(sql, type, loginName);
	}*/
	
	@Override
	public AdminUserEntity getUserByAccount(String loginName) {
		String sql = "select * from p2p_admin_user where account = ? ";
		return this.findEntity(sql, AdminUserEntity.class,loginName);
	}
	
	@Override
	public AdminUserEntity queryAdminUserByLogin(String account, String password) {
		String sql = "select * from p2p_admin_user where account=? and password=? ";
		return this.findEntity(sql, AdminUserEntity.class, account, password);
	}

	@Override
	public PageResult<Object[]> findPageAdminUserEntity(AdminUserVO vo) {
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, vo.getPageSize(), vo.getShowPage());
		StringBuilder sql = new StringBuilder();

		sql.append(
				"SELECT admin.id, NAME, mobile,  CASE WHEN STATUS = 0 THEN '启用' ELSE '停用' END AS STATUS, admin.create_time FROM p2p_admin_user admin LEFT JOIN p2p_admin_role role ON admin.role_id = role.id where 1=1  ");
		// 创建时间
		if (StringUtil.isNotBlank(vo.getBeginCreateTime())) {

			sql.append(" and date_format(admin.create_time,'%Y-%m-%d') >= ?");
			sqlPageQuery.addParam(vo.getBeginCreateTime());
		}
		if (StringUtil.isNotBlank(vo.getEndCreateTime())) {

			sql.append(" and date_format(admin.create_time,'%Y-%m-%d') <= ?");
			sqlPageQuery.addParam(vo.getEndCreateTime());
		}

		// 名字
		if (StringUtil.isNotBlank(vo.getName())) {
			sql.append(" and admin.name like ?");
			sqlPageQuery.addParam("%" + vo.getName() + "%");
		}
      //手机号
		if (StringUtil.isNotBlank(vo.getMobile())) {
			sql.append(" and admin.mobile like ?");
			sqlPageQuery.addParam("%" + vo.getMobile() + "%");
		}
		

		// 状态
		if (StringUtil.isNotBlank(vo.getStatus()) ) {

			sql.append(" and admin.status = ?");
			sqlPageQuery.addParam(vo.getStatus());
		}

		sql.append("  ORDER BY admin.create_time DESC  ");
		sqlPageQuery.setQuerySql(sql.toString());
		PageResult<Object[]> page = this.findDataWithPage(sqlPageQuery);
		return page;

	}

	@Override
	public AdminUserEntity queryAdminUserById(String id) {

		return this.getById(AdminUserEntity.class, id);
	}

	@Override
	public String saveAdminUser(AdminUserEntity admin) {
		Date date = new Date();
		admin.setCreateTime(date);
		admin.setUpdateTime(date);

		boolean flag = this.insert(admin);
		if (flag == true) {
			return admin.getId();
		} else {
			return null;
		}
	}

	@Override
	public boolean updateAdminUser(AdminUserEntity admin) {

		Date date = new Date();
		admin.setUpdateTime(date);

		return this.updateById(admin);
	}

	@Override
	public List<AdminRoleEntity> queryRoleList(){
		return this.findEntitys("select * from p2p_admin_role ", AdminRoleEntity.class);
	}
	
	
	@Override
	public PageResult<Object[]> queryRoleListByUser(AdminUserVO vo){
		SqlPageQuery<Object[]> page=new SqlPageQuery<>(Object[].class, vo.getPageSize(), vo.getShowPage());
		StringBuffer sql=new StringBuffer();
		sql.append("select r.id ,r.role_code,r.role_name,r.description from p2p_admin_role r"
				+ " left join p2p_admin_user_role a on r.id = a.role_id where 1=1 ");
		if(com.eascs.common.util.StringUtil.isNotBlank(vo.getId())){
			sql.append(" and a.admin_id = ?");
			page.addParam(vo.getId());
		}
		page.setQuerySql(sql.toString());
		PageResult<Object[]> data = this.findDataWithPage(page);
		return data;
	}
	
	
	@Override
	public boolean isExistUserRole(String userId,String roleId){
		String id = this.findString("select id from p2p_admin_user_role where role_id= ? and admin_id = ?", roleId,userId);
		if(com.eascs.common.util.StringUtil.isNotBlank(id)){
			return true;
		}else{
			return false;
		}
	}
	
	@Override
	public String saveUserRole(AdminUserRoleEntity adminUserRoleEntity){
		Date date = new Date();
		adminUserRoleEntity.setCreateTime(date);
		adminUserRoleEntity.setUpdateTime(date);

		boolean flag = this.insert(adminUserRoleEntity);
		if (flag == true) {
			return adminUserRoleEntity.getId();
		} else {
			return null;
		}
	}
	
	@Override
	public boolean delUserRole(String userId,String roleId){
		return this.execute("delete from p2p_admin_user_role where role_id= ? and admin_id = ?", roleId,userId);
	}
	
	@Override
	public Boolean checkIsAdmin(String userId) {
		return this.checkUserRole(userId, AdminRoleEntity.ROLE_ADMIN_CODE);
	}
	
	@Override
	public Boolean checkUserRole(String userId, String roleCode) {
		String sql = "select count(*) from p2p_admin_user_role p inner join p2p_admin_role  r on p.role_id=r.id where p.admin_id= ? and r.id= ? ";
		int count =  super.findInteger(sql, userId,roleCode);
		if(count > 0){
			return true;
		}
		return false;
	}
	
}
